package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.GetBetsPagingAction;
import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.server.data.WebetekUser;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.shared.BetBean;
import com.aptemo.webetek.shared.PagingResult;

public class BetsPagingHandler
		extends ActionHandler<GetBetsPagingAction, PagingResult<BetBean>>
{

	public BetsPagingHandler(DataStoreAPI dataService)
	{
		super(dataService);
	}

	public Class<? extends Action<?>> getActionType()
	{
		return GetBetsPagingAction.class;
	}

	@Override
	public PagingResult<BetBean> invoke(GetBetsPagingAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();

		String teamId = action.getTeamId();
		EnumBetStatus status = action.getStatus();
		String cursor = action.getCursor();
		int pageSize = action.getPageSize();

		// team member?
		WebetekUser user = utils.getCurrentUser(action.getTeamId());
		if (user == null)
		{
			throw new BackendException(EErrorCodes.NOT_TEAM_MEMBER);
		}
		String userId = user.getSid();

		return dataService.getBets(teamId, userId, status, cursor, pageSize);
	}
}
